package com.lovestudying.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovestudying.service.LikeListService;
import com.lovestudying.service.impl.LikeListServiceImpl;

/**
 * Servlet implementation class LikeListServlet
 */
@WebServlet("/LikeListServlet")
public class LikeListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	LikeListService likeListService = new LikeListServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LikeListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		switch (op) {
		case "addALike":
			addALike(request, response);
			break;
			
		case "cancelLike":
			cancelLike(request, response);
			break;
		default:
			break;
		}
	}

	private void cancelLike(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String objId = request.getParameter("objId");
		String accountId = request.getParameter("accountId");
		if (likeListService.cancelLike(accountId, objId)) {
			response.getWriter().print(true);
		} else {
			response.getWriter().print(false);
		}

	}

	private void addALike(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String objId = request.getParameter("objId");
		String accountId = request.getParameter("accountId");
		if (likeListService.addALike(accountId, objId)) {
			response.getWriter().print(true);
		} else {
			response.getWriter().print(false);
		}

	}

}
